package cn.nvrelax.cloudnote.gateway.http;

import cn.nvrelax.cloudnote.dao.UserDao;
import cn.nvrelax.cloudnote.entity.User;
import cn.nvrelax.cloudnote.entity.UserExample;
import cn.nvrelax.cloudnote.syskit.JsonTool;
import cn.nvrelax.cloudnote.syskit.SysState;
import cn.nvrelax.cloudnote.syskit.TokenUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 程序描述：用户相关调用接口。注册，登录
 * 节省时间，参数不做合法校验，不考虑安全性。不考虑接口暴露风险
 * 创建时间：2019/6/3 14:08
 * 编码：nvRelax
 */
@RestController
@Log4j2
public class UserServices {

    @Autowired
    UserDao userDao;
    @Autowired
    TokenUtil tokenUtil;

    @PostMapping("/start/user/register")
    public JSONObject register(@RequestParam User user) {
        JSONObject json;
        UserExample example = new UserExample();
        example.createCriteria().andEmailEqualTo(user.getEmail());
        if (userDao.selectByExample(example).size() != 0) {
            json = JsonTool.stateError("用户已存在！");
            return json;
        }
        user.setDate(new Date());
        int res = userDao.insertSelective(user);
        if (res != 1) {
            json = JsonTool.stateError("参数异常！");
        } else {
            json = JsonTool.stateOK(null);
            json.put("user",user);
        }
        return json;
    }

    @PostMapping("/start/user/login")
    public JSONObject login(@RequestParam User loginMsg) {
        User user;
        UserExample example = new UserExample();
        switch (loginMsg.getType()) {
            case "default":
                example.createCriteria().andEmailEqualTo(loginMsg.getEmail())
                        .andPasswordEqualTo(loginMsg.getPassword());
                break;
            case "QQ":
                example.createCriteria().andOtherkeyEqualTo(loginMsg.getOtherkey());
                break;
        }
        List<User> list = userDao.selectByExample(example);
        if (list.size() == 0) {
            return JsonTool.stateError("不存在对应的用户！");
        }
        user = list.get(0);
        Map<String,Object> signBody = new HashMap<>();
        signBody.put("id",user.getId());
        signBody.put("nickName",user.getNickname());
        signBody.put("sex",user.getSex());
        signBody.put("type",user.getType());
        JSONObject json = JsonTool.stateOK(null);
        String token = tokenUtil.signAppToken(signBody);
        json.put("token", token);
        SysState.recordUser(token, user.getId());
        return json;
    }

    /**
     * 更改用户信息
     * @param user 你猜
     * @param id 你猜
     * @return 你猜
     */
    @PostMapping("/api/user/update")
    public JSONObject update(@RequestParam User user, @RequestAttribute("authUser")Integer id) {
        if (user.getId().intValue() != id) {
            return null;
        }
        userDao.updateByPrimaryKeySelective(user);
        JSONObject json = JsonTool.stateOK(null);
        json.put("user",user);
        return json;
    }

    @GetMapping("/api/user/online")
    public List<User> selectOnlineUser() {
        UserExample example = new UserExample();
        example.createCriteria().andIdIn(Arrays.asList(SysState.userIdList()));
        return userDao.selectByExample(example);
    }
}
